<template>
  <!-- 注册页面 -->
  <div class="login">
    <div class="login-item">
      <div class="login-login">
        <h3 class="login-h3">注册</h3>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm">
          <el-form-item label="账号" prop="stu_id">
            <el-input prefix-icon="el-icon-user-solid " type="text" v-model="ruleForm.stu_id"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="user">
            <el-input prefix-icon="el-icon-user-solid " type="text" v-model="ruleForm.user"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="paw">
            <el-input prefix-icon="el-icon-lock" type="password" v-model="ruleForm.paw" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input prefix-icon="el-icon-lock" type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="register('ruleForm')" type="primary" class="login-input">注册</el-button>
          </el-form-item>
        </el-form>
        <div class="login-register">
          <span @click="Login" class="register-item">快速登录</span>
          <i class="register-i">|</i>
          <span class="register-item">忘记密码</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Register } from '../../network/login'
import { setToken } from '../../cookie/index'
export default {
  name: 'Register',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.paw) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        stu_id: '',
        user: '',
        paw: '',
        checkPass: ''
      },
      rules: {
        stu_id: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6到16位字符', trigger: 'blur' }
        ],
        user: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入3到8位用户名', trigger: 'blur' }
        ],
        paw: [
          { validator: validatePass, trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6到16位密码', trigger: 'blur' }
        ],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    register(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          Register(
            `stu_id=${this.ruleForm.stu_id}&paw=${this.ruleForm.paw}&user=${this.ruleForm.user}&checkPass=${this.ruleForm.checkPass}`
          ).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '注册成功',
                type: 'success'
              })
              setToken(res.dataName)
              this.$router.push('/')
            } else if (res.code === 500) {
              this.$message.error('账号已存在，请换账号重新注册')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    Login() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.login {
  height: 100vh;
  background: url(../../assets/img/swiper/1.jpg) no-repeat;
  background-size: 100% 100%;
}
.login-item {
  width: 100%;
  height: 100%;
  background-color: rgba(100, 100, 100, 0.5);
  overflow: hidden;
}
.login-login {
  position: relative;
  top: 50%;
  left: 50%;
  width: 30%;
  height: 800px;
  transform: translate(-50%, -50%);
  overflow: hidden;
}
.login-h3 {
  font-size: 40px;
  font-weight: 500;
  text-align: center;
  margin-bottom: 30px;
  margin-top: 20px;
}
.login-input {
  display: block;
  width: 80%;
  height: 50px;
  margin: 0 auto;
  border-radius: 20px;
  border: 0;
  outline: none;
  margin-bottom: 15px;
  box-sizing: border-box;
}
.login-input:nth-child(-n + 2) {
  padding-left: 40px;
  background-color: rgba(100, 100, 100, 0);
  border: 1px solid #fff;
}

.login-input:nth-child(3) {
  background-color: #f05050;
  font-size: 20px;
  color: #fff;
}
.login-register {
  text-align: center;
}
.register-item {
  font-size: 16px;
  cursor: pointer;
}
.register-item:hover {
  color: #000;
}
.register-i {
  padding: 0 10px;
}
</style>